<?php

namespace Console\Logic;

use Common\Logic\BaseLogic;

class JoinLogic extends BaseLogic
{
    private $joinModel;

    public function __construct()
    {
        parent::__construct();
        $this->joinModel = D('Join');
    }

    /**
     * 根据条件统计
     *
     * @param array $where
     *
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->joinModel->where($where)->count();
    }

    /**
     * 获取加入列表.
     *
     * @param int $page
     * @param int $size
     * @param $where
     *
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where)
    {
        $list = $this->joinModel->where($where)->page($page, $size)->order('id desc')->select();
        $total = $this->statistics($where);

        return static::$res->suc('成功', array(
            'list' => $list,
            'total' => $total,
        ));
    }

    /**
     * 获取任务id列表.
     *
     * @param $where
     *
     * @return mixed
     */
    public function getTaskIdList($where)
    {
        $list = $this->joinModel->field('task_id')->where($where)->group('task_id')->order('id desc')->select();

        return static::$res->suc('成功', $list);
    }

    /**
     * 获取任务明细.
     *
     * @param $where
     *
     * @return mixed
     */
    public function getInfo($where)
    {
        $info = $this->joinModel->where($where)->find();

        return static::$res->suc('成功', $info);
    }

    /**
     * 审核任务
     *
     * @param $usrId
     * @param $id
     * @param $state
     *
     * @return mixed
     */
    public function auditJoin($usrId, $id, $state)
    {
        if (empty($usrId)) {
            return static::$res->err('审核人ID不能为空');
        }

        if (empty($id)) {
            return static::$res->err('参与凭证不能为空');
        }

        if (3 != $state && -2 != $state) {
            return static::$res->err('审核状态不支持');
        }

        $count = $this->joinModel->where(array(
            'id' => $id,
            "state" => array('neq', -1)
        ))->save(array(
            'state' => $state,
            'audit_user_id' => $usrId,
            'audit_time' => date('Y-m-d H:i:s'),
        ));

        if (empty($count)) {
            return static::$res->err('数据库异常审核失败');
        }

        if (3 == $state) {
            //给用户发放奖金
            $incomeLogic = A('Console/Income', 'Logic');
            $addRes = $incomeLogic->addTaskIncome($id);
            if (static::$res->isErr($addRes)) {
                //写日志
                $addRes['joinId'] = $id;
                $fileName = RUNTIME_PATH.'Logs/incomeErr/'.date('Y-m-d').'.log';
                logs($addRes, $fileName);
            }
        }

        return static::$res->suc('审核成功');
    }

    /**
     * 添加测试账号.
     *
     * @param [type] $taskId
     * @param [type] $count
     *
     * @return void
     */
    public function addTestUser($taskId, $count)
    {
        if (empty($taskId)) {
            return static::$res->err('任务ID不能为空');
        }

        if (empty($count)) {
            return static::$res->err('虚拟人数不能为空');
        }

        if ($count > 266) {
            $count = 266;
        }

        //获取任务信息
        $listModel = D('List');
        $taskInfo = $listModel->where("id=$taskId")->find();
        if (empty($taskInfo)) {
            return static::$res->err('未找到任务');
        }

        //查找所有参与的虚拟账号
        $joinWhere = array(
            'task_id' => $taskId,
            'is_test' => 1,
        );
        $testJoinUserList = $this->joinModel->where($joinWhere)->select();
        $oldCount = count($testJoinUserList);
        $count = $count - $oldCount;
        if ($count <= 0) {
            return static::$res->err('虚拟数小于源值');
        }

        //获取所有虚拟账号
        $userWhere = array(
            'openid' => array('like', 'test_%'),
        );

        if ($oldCount) {
            $userIds = array();
            foreach ($testJoinUserList as $vo) {
                $userIds[] = $vo['user_id'];
            }

            $userWhere['id'] = array('not in', $userIds);
        }

        $userModel = D('User');
        $testUserList = $userModel->where($userWhere)->select();

        $incomeLogic = A('Console/Income', 'Logic');

        for ($i = 0; $i < $count; ++$i) {
            $userInfo = $testUserList[$i];
            $data = array(
                'user_id' => $userInfo['id'],
                'task_id' => $taskId,
                'task_title' => $taskInfo['title'],
                'task_type' => $taskInfo['type'],
                'income' => $taskInfo['income'],
                'admin_user_id' => $taskInfo['user_id'],
                'state' => 3,
                'join_time' => date('Y-m-d H:i:s'),
                'finish_time' => date('Y-m-d H:i:s'),
                'audit_user_id' => 1000000,
                'audit_time' => date('Y-m-d H:i:s'),
                'phone' => $userInfo['phone'],
                'doing_time' => date('Y-m-d H:i:s'),
            );

            $id = $this->joinModel->add($data);
            if ($id) {
                //已抢数量+1
                $listModel->where(array(
                    'id' => $taskId,
                ))->setInc('join_num');

                //已完成数量+1
                $listModel->where(array(
                    'id' => $taskId,
                ))->setInc('finish_num');

                //给测试账号打钱
                $incomeRes = $incomeLogic->addTaskIncome($id);
            }
        }

        return static::$res->suc('成功');
    }
}
